package models

import "shop-microservices/basic/inits"

// 热度值表
type Hot struct {
	Id        uint32 `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	BrandId   uint64 `gorm:"column:brand_id;type:bigint UNSIGNED;default:NULL;" json:"brand_id"`
	BrandName string `gorm:"column:brand_name;type:varchar(255);default:NULL;" json:"brand_name"`
	Hot       uint64 `gorm:"column:hot;type:bigint UNSIGNED;default:NULL;" json:"hot"`
}

// TableName 返回表名称
func (h *Hot) TableName() string {
	return "hot"
}
func (h *Hot) GetHotBrandId() error {
	return inits.DB.Where("brand_id = ?", h.BrandId).Limit(1).Find(&h).Error
}

// AddBrandHot 添加热度
func (h *Hot) AddBrandHot() error {
	return inits.DB.Create(&h).Error
}

// Updated 修改热度值
func (h *Hot) Updated() error {
	return inits.DB.Updates(&h).Error
}

// HotList 热度搜索
func (h *Hot) HotList(limit int) (list []Hot, err error) {
	db := inits.DB.Order("hot desc")
	if limit != 0 {
		db = db.Limit(limit)
	} else {
		db = db.Limit(10)
	}
	db = db.Find(&list)
	return
}

type HotSearchGoodsList struct {
	Hot         int64   `json:"hot"`          //品牌热度
	GoodsName   string  `json:"goods_name"`   //商品名称
	Id          uint32  `json:"id"`           //商品Id
	GoodsPrice  float64 `json:"goods_price"`  //商品价格
	BrandName   string  `json:"brand_name"`   //品牌名称
	GoodsUrl    string  `json:"goods_url"`    //商品图片
	GoodsDesc   string  `json:"goods_desc"`   //商品简介
	GoodsStock  uint32  `json:"goods_stock"`  //商品库存
	GoodsStatus uint8   `json:"goods_status"` //商品状态
	Sales       int32   `json:"sales"`        //销量
	SliderImage string  `json:"slider_image"` //轮播图
}

func (h *Hot) HotSearchShopList(brandName string) (list []HotSearchGoodsList, err error) {
	inits.DB.Model(&Hot{}).Select("goods.*,hot.brand_name as brand_name,hot.hot as hot").
		Joins("join goods on goods.brand_name = hot.brand_name").
		Where("hot.brand_name like ?", "%"+brandName+"%").
		Order("goods.sales desc").
		Find(&list)
	return
}
